package org.lili.performance.chapter8;

/**
 * @author lili
 * @description ${DESCRIPTION}
 * @create 2018-03-03 下午4:51
 * @since
 **/
public class SimpleExample2 {
    final private static long ITERATIONS = 5000000000L;

    final private static long WARMUP = 10000000L;

    final private static long NANOS_PER_MS = 1000L * 1000L;

    private static boolean equalsTest(String s) {
        boolean b = s.equals(s);
        return b;
    }

    private static long doTest(long n) {
        long start = System.nanoTime();
        for (int i = 0; i < n; i++) {
            equalsTest("ABC");
        }
        long end = System.nanoTime();
        return end - start;
    }

    public static void printStats(long n, long nanos) {
        float itrsPerMs = 0;
        float millis = nanos / NANOS_PER_MS;
        if (millis != 0) {
            itrsPerMs = n / (nanos / NANOS_PER_MS);
        }
        System.out.println("    Elapsed time in ms ->" + millis);
        System.out.println("    Iterations / ms ---->" + itrsPerMs);
    }

    public static void main(String[] args) {
        System.out.println("Warming up....");
        long nanos = doTest(WARMUP);
        System.out.println("1st Warmup done.");
        printStats(WARMUP, nanos);

        System.out.println("Starting 2nd Warming up....");
        nanos = doTest(WARMUP);
        System.out.println("2st Warmup done.");
        printStats(WARMUP, nanos);


        System.out.println("Starting mesurement interval ...");
        nanos = doTest(900000000L);
        System.out.println("Measurement interval done.");
        System.out.println("Test Completed");
        printStats(ITERATIONS, nanos);
    }
}
